/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import pojo.PhieuNhap;
import util.HibernateUtil;

/**
 *
 * @author LaiHuynh
 */
public class PhieuNhapDAO {
     public static List<PhieuNhap> layDanhSachPhieuNhap() {
        List<PhieuNhap> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select pn from PhieuNhap pn";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }   
        return ds;        
    }
    
    public static PhieuNhap layThongTinPhieuNhap(int maPhieuNhap) {
        PhieuNhap pn = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            pn = (PhieuNhap) session.get(PhieuNhap.class, maPhieuNhap);
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return pn;
    }
    
    public static void themPhieuNhap(PhieuNhap pn) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(pn);
        session.getTransaction().commit();
        session.close(); 
    }
    public static int LayMa(){
         int kq=0;
        
        Session session = HibernateUtil.getSessionFactory().openSession();
         String hql = "select max(pn.maPhieuNhap) from PhieuNhap pn ";
         org.hibernate.Query query = session.createQuery(hql);
         kq =(Integer) query.uniqueResult(); 
         session.close();
        return kq;
    }
}
